/* hal_lptmr.c */
#include "hal_lptmr.h"




void LPTMR_InitTimeCounter(lpTMR_Type * base, LPTMR_TimeCounterInit_Type * init)
{
    base->CTRL = 0u; /* disable the lptmr before any setting. */

    uint32_t prs = 0u;
    if (init->ClkDiv == LPTMR_ClkDiv_Bypass)
    {
        prs =lpTMR_PRS_BYPASS_MASK;
    }
    else
    {
        prs = lpTMR_PRS_PRES(init->ClkDiv);
    }
    base->PRS = prs;

    base->CMP = init->ModVal;
}



void LPTMR_InitPulseCounter(lpTMR_Type * base, LPTMR_PulseCounterInit_Type * init)
{
    base->CTRL = 0; /* disable the lptmr before any setting. */
    base->CTRL = lpTMR_CTRL_PINSEL(init->PulseInputSrc)
               | lpTMR_CTRL_PINPOL(init->PulseInputPol)
               | lpTMR_CTRL_MODE(1) /* counter mode. */
               ;
    
    uint32_t prs = 0u;
    if (init->PulseInputGlitch == LPTMR_PulseInputGlitch_Bypass)
    {
        prs =lpTMR_PRS_BYPASS_MASK;
    }
    else
    {
        prs = lpTMR_PRS_PRES(init->PulseInputGlitch);
    }
    base->PRS = prs;
    
    base->CMP = init->ModVal;
}

uint32_t LPTMR_GetCounterValue(lpTMR_Type * base)
{
    return base->CNT;
}

void LPTMR_EnableInterrupts(lpTMR_Type * base, uint32_t flags)
{
    if ( (flags &= LPTMR_FLAG_MATCH_MOD) != 0u )
    {
        base->DIE |= LPTMR_FLAG_MATCH_MOD;
    }
}

void LPTMR_DisableInterrupts(lpTMR_Type * base, uint32_t flags)
{
    if ( (flags &= LPTMR_FLAG_MATCH_MOD) != 0u )
    {
        base->DIE &= ~LPTMR_FLAG_MATCH_MOD;
    }
}

void LPTMR_EnableDma(lpTMR_Type * base, bool enable)
{
    if (enable)
    {
        base->DIE |= lpTMR_DIE_DMAEN_MASK;
    }
    else
    {
        base->DIE  &= ~lpTMR_DIE_DMAEN_MASK;
    }
}

uint32_t LPTMR_GetStatusFlags(lpTMR_Type * base)
{
    return base->STS;
}

void LPTMR_ClearStatusFlags(lpTMR_Type * base, uint32_t flags)
{
    base->STS = flags;
}

void LPTMR_StartCounter(lpTMR_Type * base)
{
    base->CTRL |= lpTMR_CTRL_EN_MASK;
}

void LPTMR_StopCounter(lpTMR_Type * base)
{
    base->CTRL &= ~lpTMR_CTRL_EN_MASK;
}

/* EOF. */

